package com.ts.api.module.api.convert.core.utils;

import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.entity.ApiUserRequest;

/**
 *  API机构/平台字段转化器
 *
 *  @author Pan
 */
public class ApiFieldConverter {

    private final ApiUserRequest apiUserRequest;

    private ApiFieldConverter(ApiDataExchangeParameter apiDataExchangeParameter) {
        this.apiUserRequest = CopyUtils.copy(apiDataExchangeParameter, ApiUserRequest.class);
    }

    private ApiFieldConverter(ApiApplyParameter apiApplyParameter) {
        this.apiUserRequest = CopyUtils.copy(apiApplyParameter, ApiUserRequest.class);
    }

    /**
     *  房产情况
     *  有/无
     *  @return
     */
    public String hasRealEstateInfoToStr() {
        //  房产情况
        if (ValidParam.isNotEmpty(apiUserRequest.getRealEstateInfo())) {
            String text = ChooseEq.create()
                .when(apiUserRequest.getRealEstateInfo(), "1", "无")
            .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  房产情况
     *  0无/1有
     *  @return
     */
    public Integer hasRealEstateInfoToNumber() {
        //  房产情况
        if (ValidParam.isNotEmpty(apiUserRequest.getRealEstateInfo())) {
            Integer text = ChooseEq.create()
                .when(apiUserRequest.getRealEstateInfo(), "1", 0)
            .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  有无车产情况
     *
     *  @return
     */
    public String hasCarInfoToStr() {
        //  有无车产情况
        if (ValidParam.isNotEmpty(apiUserRequest.getCarInfo())) {
            String text = ChooseEq.create()
                .when(apiUserRequest.getCarInfo(), "1", "无")
            .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  有无车产情况
     *  0无
     *  @return
     */
    public Integer hasCarInfoToNumber() {
        //  有无车产情况
        if (ValidParam.isNotEmpty(apiUserRequest.getCarInfo())) {
            Integer text = ChooseEq.create()
                .when(apiUserRequest.getCarInfo(), "1", 0)
            .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  有无保单情况
     *
     *  @return
     */
    public String hasInsurancePolicyToStr() {
        //  保单
        if (ValidParam.isNotEmpty(apiUserRequest.getInsurancePolicy())) {
            String text = ChooseEq.create()
                .when(apiUserRequest.getInsurancePolicy(), "1", "无")
            .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  有无保单情况
     *  0无
     *  @return
     */
    public Integer hasInsurancePolicyToNumber() {
        //  保单
        if (ValidParam.isNotEmpty(apiUserRequest.getInsurancePolicy())) {
            Integer text = ChooseEq.create()
                .when(apiUserRequest.getInsurancePolicy(), "1", 0)
            .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  有无社保情况
     *
     *  @return
     */
    public String hasSocialSecurityToStr() {
        //  社保
        if (ValidParam.isNotEmpty(apiUserRequest.getSocialSecurity())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getSocialSecurity(), "1", "无")
                    .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  有无社保情况  1有/0无
     *  @return
     */
    public Integer hasSocialSecurityToNumber() {
        //  社保
        if (ValidParam.isNotEmpty(apiUserRequest.getSocialSecurity())) {
            Integer text = ChooseEq.create()
                    .when(apiUserRequest.getSocialSecurity(), "1", 0)
                    .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  有无公积金情况
     *
     *  @return
     */
    public String hasProvidentFundToStr() {
        //  公积金
        if (ValidParam.isNotEmpty(apiUserRequest.getProvidentFund())) {
            String text = ChooseEq.create()
                .when(apiUserRequest.getProvidentFund(), "1", "无")
            .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  有无公积金情况
     *  0无
     *  @return
     */
    public Integer hasProvidentFundToNumber() {
        //  公积金
        if (ValidParam.isNotEmpty(apiUserRequest.getProvidentFund())) {
            Integer text = ChooseEq.create()
                .when(apiUserRequest.getProvidentFund(), "1", 0)
            .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  银行代发情况
     *
     *  @return
     */
    public String hasSalaryBankToStr() {
        //  银行代发情况
        if (ValidParam.isNotEmpty(apiUserRequest.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getSalaryPayMethod(), "1", "无")
                    .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  是否银行代发情况
     *  0无
     *  @return
     */
    public Integer hasSalaryBankToNumber() {
        //  银行代发情况
        if (ValidParam.isNotEmpty(apiUserRequest.getSalaryPayMethod())) {
            Integer text = ChooseEq.create()
                .when(apiUserRequest.getSalaryPayMethod(), "1", 0)
            .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  银行代发情况,是/否
     *
     *  @return
     */
    public String hasSalaryBankChooseToStr() {
        //  银行代发情况
        if (ValidParam.isNotEmpty(apiUserRequest.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getSalaryPayMethod(), "1", "否")
                    .end("是");
            return text;
        }
        return "无";
    }

    /**
     *  有无职业
     *
     *  @return
     */
    public String hasCareerToStr() {
        return ValidParam.isNotEmpty(apiUserRequest.getCareer()) ? "有" : "无";
    }

    /**
     *  有无职业
     *  0无
     *  @return
     */
    public Integer hasCareerToNumber() {
        return ValidParam.isNotEmpty(apiUserRequest.getCareer()) ? 1 : 0;
    }

    /**
     *  信用卡-有/无
     *
     *  @return String
     */
    public String hasCreditLimitToStr() {
        //  信用卡
        if (ValidParam.isNotEmpty(apiUserRequest.getCreditLimit())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getCreditLimit(), "1", "无")
                    .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  信用卡- 1有/0无
     *
     *  @return Integer
     */
    public Integer hasCreditLimitToNumber() {
        //  信用卡
        if (ValidParam.isNotEmpty(apiUserRequest.getCreditLimit())) {
            Integer text = ChooseEq.create()
                    .when(apiUserRequest.getCreditLimit(), "1", 0)
                    .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  营业执照
     *
     *  @return
     */
    public String hasBizLicenseYearToStr() {
        //  营业执照
        if (ValidParam.isNotEmpty(apiUserRequest.getBizLicenseYear())) {
            String text = ChooseEq.create()
                .when(apiUserRequest.getBizLicenseYear(), "1", "无")
            .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  营业执照
     *  0无
     *  @return
     */
    public Integer hasBizLicenseYearToNumber() {
        //  营业执照
        if (ValidParam.isNotEmpty(apiUserRequest.getBizLicenseYear())) {
            Integer text = ChooseEq.create()
                .when(apiUserRequest.getBizLicenseYear(), "1", 0)
            .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  是否逾期-有逾期/无逾期
     *
     *  @return
     */
    public String hasOverdueToStr() {
        //  是否逾期
        if (ValidParam.isNotEmpty(apiUserRequest.getOverdue())) {
            String text = ChooseEq.create()
                .when(apiUserRequest.getOverdue(), "1", "无")
            .end("有");
            return text;
        }
        return "无";
    }

    /**
     *  是否逾期-1有逾期/0无逾期
     *  @return
     */
    public Integer hasOverdueToNumber() {
        //  营业执照
        if (ValidParam.isNotEmpty(apiUserRequest.getOverdue())) {
            Integer text = ChooseEq.create()
                .when(apiUserRequest.getOverdue(), "1", 0)
            .end(1);
            return text;
        }
        return 0;
    }

    /**
     *  有无月收入,文字 形式
     *  无 有
     *
     *  @return
     */
    public String hasMonthlySalaryToStr() {
        //  收入
        return ValidParam.isNotEmpty(apiUserRequest.getMonthlySalary()) ? "有" : "无";
    }

    /**
     *  有无月收入,数值形式
     *  0无 1有
     *
     *  @return
     */
    public Integer hasMonthlySalaryToNumber() {
        //  收入
        return ValidParam.isNotEmpty(apiUserRequest.getMonthlySalary()) ? 1 : 0;
    }

    /**
     *  获取月收入,数值形式
     *
     *  @return
     */
    public Integer getMonthlySalaryNumber() {
        Integer monthlySalary = ChooseEq.create()
            .when(apiUserRequest.getMonthlySalary(), "1", 3000)
            .when(apiUserRequest.getMonthlySalary(), "2", 8000)
            .when(apiUserRequest.getMonthlySalary(), "3", 15000)
            .when(apiUserRequest.getMonthlySalary(), "4", 30000)
            .when(apiUserRequest.getMonthlySalary(), "5", 50000)
        .end();
        return monthlySalary;
    }

    /**
     *  贷款额度
     *  带单位
     *  @return
     */
    public String getLoanAmountUnit() {
        if (ValidParam.isNotEmpty(apiUserRequest.getLoanAmount())) {
            String money = ChooseEq.create()
                .when(apiUserRequest.getLoanAmount(), "1", "3万")
                .when(apiUserRequest.getLoanAmount(), "2", "5万")
                .when(apiUserRequest.getLoanAmount(), "3", "10万")
                .when(apiUserRequest.getLoanAmount(), "4", "20万")
            .end();
            return money;
        }
        return null;
    }

    /**
     *  贷款额度
     *  带单位
     *  @return
     */
    public Integer getLoanAmountInt() {
        if (ValidParam.isNotEmpty(apiUserRequest.getLoanAmount())) {
            Integer money = ChooseEq.create()
                .when(apiUserRequest.getLoanAmount(), "1", 30000)
                .when(apiUserRequest.getLoanAmount(), "2", 50000)
                .when(apiUserRequest.getLoanAmount(), "3", 100000)
                .when(apiUserRequest.getLoanAmount(), "4", 200000)
            .end();
            return money;
        }
        return 30000;
    }

    /**
     *  获取年龄
     *
     *  @return Integer
     */
    public Integer getAgeInt() {
        if (ValidParam.isNull(apiUserRequest.getAge())) {
            return null;
        }
        //  非年龄字典（1、2、3）
        return Integer.valueOf(apiUserRequest.getAge());
    }

    /**
     *  获取年龄
     *  @return String
     */
    public String getAge() {
        return apiUserRequest.getAge();
    }

    /**
     *  获取性别
     *  中文
     *
     *  @return String
     */
    public String getSexText() {
        //  性别
        String sex = ChooseEq.create()
            .when(apiUserRequest.getSex(), "0", "男")
            .when(apiUserRequest.getSex(), "1", "女")
        .end("");
        return sex;
    }

    /**
     *  获取芝麻分文字形式
     *
     *  @return
     */
    public String getSesameCreditText() {
        String zhima = ChooseEq.create()
            .when(apiUserRequest.getSesameCredit(), "1", "600分以下")
            .when(apiUserRequest.getSesameCredit(), "2", "600~649分")
            .when(apiUserRequest.getSesameCredit(), "3", "650~699分")
            .when(apiUserRequest.getSesameCredit(), "4", "700分以上")
        .end();
        return zhima;
    }

    /**
     *  获取芝麻分，分值形式(整型)
     *  @return
     */
    public Integer getSesameCreditNumber() {
        Integer zhima = ChooseEq.create()
                .when(apiUserRequest.getSesameCredit(), "1", 600)
                .when(apiUserRequest.getSesameCredit(), "2", 649)
                .when(apiUserRequest.getSesameCredit(), "3", 699)
                .when(apiUserRequest.getSesameCredit(), "4", 700)
        .end();
        return zhima;
    }

    /**
     *  获取芝麻分，分值形式(字符串)
     *  @return
     */
    public Integer getSesameCreditStr() {
        Integer zhima = ChooseEq.create()
            .when(apiUserRequest.getSesameCredit(), "1", "600")
            .when(apiUserRequest.getSesameCredit(), "2", "649")
            .when(apiUserRequest.getSesameCredit(), "3", "699")
            .when(apiUserRequest.getSesameCredit(), "4", "700")
        .end();
        return zhima;
    }

    /**
     *  获取花呗文字形式
     *
     *  @return String
     */
    public String getHbLimitText() {
        //  花呗
        String huaBei = ChooseEq.create()
            .when(apiUserRequest.getHbLimit(), "1", "无额度")
            .when(apiUserRequest.getHbLimit(), "2", "3千以下")
            .when(apiUserRequest.getHbLimit(), "3", "3千-1万")
            .when(apiUserRequest.getHbLimit(), "4", "1-3万")
            .when(apiUserRequest.getHbLimit(), "5", "3万以上")
        .end("无额度");
        return huaBei;
    }

    /**
     *  获取白条文字形式
     *
     *  @return String
     */
    public String getBtLimitText() {
        //  白条余额
        String btLimit = ChooseEq.create()
            .when(apiUserRequest.getBtLimit(), "1", "无额度")
            .when(apiUserRequest.getBtLimit(), "2", "3千以上")
            .when(apiUserRequest.getBtLimit(), "3", "3千-1万")
            .when(apiUserRequest.getBtLimit(), "4", "1-3万")
            .when(apiUserRequest.getBtLimit(), "5", "3万以上")
        .end("无额度");
        return btLimit;
    }

    /**
     *  获取学历-文字形式
     *
     *  @return
     */
    public String getEducationAttainmentText() {
        //  学历
        String text = ChooseEq.create()
            .when(apiUserRequest.getEducationAttainment(), "1", "初中及以下")
            .when(apiUserRequest.getEducationAttainment(), "2", "中专")
            .when(apiUserRequest.getEducationAttainment(), "3", "高中")
            .when(apiUserRequest.getEducationAttainment(), "4", "大专")
            .when(apiUserRequest.getEducationAttainment(), "5", "本科")
            .when(apiUserRequest.getEducationAttainment(), "6", "研究生及以上")
        .end();
        return text;
    }

    /**
     *  获取职业-文字形式
     *
     *  @return
     */
    public String getCareerText() {
        //  职业
        String text = ChooseEq.create()
            .when(apiUserRequest.getCareer(), "1", "上班族")
            .when(apiUserRequest.getCareer(), "2", "自由职业")
            .when(apiUserRequest.getCareer(), "3", "企业主（有营业执照）")
            .when(apiUserRequest.getCareer(), "4", "公务员或事业单位")
        .end();
        return text;
    }

    /**
     *  获取拓展信息
     *
     *  @return String
     */
    public String getExtendInfo() {
        StringBuilder stringBuilder = new StringBuilder();
        //  信用记录-逾期记录
        if (ValidParam.isNotEmpty(apiUserRequest.getOverdue())) {
            String text = ChooseEq.create()
                .when(apiUserRequest.getOverdue(), "1", "无逾期")
                .when(apiUserRequest.getOverdue(), "2", "有逾期")
                .when(apiUserRequest.getOverdue(), "3", "一年以内无逾期")
            .end();
            stringBuilder.append("信用记录：").append(text).append(";").append("\n");
        }

        //  芝麻分
        if (ValidParam.isNotEmpty(apiUserRequest.getSesameCredit())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getSesameCredit(), "1", "600分以下")
                    .when(apiUserRequest.getSesameCredit(), "2", "600~649分")
                    .when(apiUserRequest.getSesameCredit(), "3", "650~699分")
                    .when(apiUserRequest.getSesameCredit(), "4", "700分以上")
                    .end();
            stringBuilder.append("芝麻分：").append(text).append(";").append("\n");
        }

        //  职业
        if (ValidParam.isNotEmpty(apiUserRequest.getCareer())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getCareer(), "1", "上班族")
                    .when(apiUserRequest.getCareer(), "2", "自由职业")
                    .when(apiUserRequest.getCareer(), "3", "企业主（有营业执照）")
                    .when(apiUserRequest.getCareer(), "4", "公务员或事业单位")
                    .end();
            stringBuilder.append("职业：").append(text).append(";").append("\n");
        }

        //  学历
        if (ValidParam.isNotEmpty(apiUserRequest.getEducationAttainment())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getEducationAttainment(), "1", "初中及以下")
                    .when(apiUserRequest.getEducationAttainment(), "2", "中专")
                    .when(apiUserRequest.getEducationAttainment(), "3", "高中")
                    .when(apiUserRequest.getEducationAttainment(), "4", "大专")
                    .when(apiUserRequest.getEducationAttainment(), "5", "本科")
                    .when(apiUserRequest.getEducationAttainment(), "6", "研究生及以上")
                    .end();
            stringBuilder.append("学历：").append(text).append(";").append("\n");
        }

        //  收入形式
        if (ValidParam.isNotEmpty(apiUserRequest.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getSalaryPayMethod(), "1", "银行代发打卡")
                    .when(apiUserRequest.getSalaryPayMethod(), "2", "现金收入")
                    .when(apiUserRequest.getSalaryPayMethod(), "3", "个人转账")
                    .end();
            stringBuilder.append("收入形式：").append(text).append(";").append("\n");
        }

        //  客户类型
        String career = apiUserRequest.getCareer();
        if (ValidParam.isEmpty(career)) {
            //  无数据为个人用户
            stringBuilder.append("客户类型：").append("个人").append(";").append("\n");
        } else {
            String text = ChooseEq.create()
                    //  如果是企业就为企业用户
                    .when(career, "3", "企业")
                    //  个人用户
                    .end("个人");
            stringBuilder.append("客户类型：").append(text).append(";").append("\n");
        }

        //  房产情况
        if (ValidParam.isNotEmpty(apiUserRequest.getRealEstateInfo())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getRealEstateInfo(), "1", "无")
                    .end("有");
            stringBuilder.append("房产情况：").append(text).append(";").append("\n");
        }

        //  车辆情况
        if (ValidParam.isNotEmpty(apiUserRequest.getCarInfo())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getCarInfo(), "1", "无")
                    .end("有");
            stringBuilder.append("车辆情况：").append(text).append(";").append("\n");
        }

        //  保单
        if (ValidParam.isNotEmpty(apiUserRequest.getInsurancePolicy())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getInsurancePolicy(), "1", "无")
                    .end("有");
            stringBuilder.append("保单：").append(text).append(";").append("\n");
        }

        //  社保
        if (ValidParam.isNotEmpty(apiUserRequest.getSocialSecurity())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getSocialSecurity(), "1", "无")
                    .end("有");
            stringBuilder.append("社保：").append(text).append(";").append("\n");
        }

        //  公积金
        if (ValidParam.isNotEmpty(apiUserRequest.getProvidentFund())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getProvidentFund(), "1", "无")
                    .end("有");
            stringBuilder.append("公积金：").append(text).append(";").append("\n");
        }

        //  银行代发
        if (ValidParam.isNotEmpty(apiUserRequest.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiUserRequest.getSalaryPayMethod(), "1", "是")
                    .end("否");
            stringBuilder.append("银行代发：").append(text).append(";").append("\n");
        }
        //  车牌号
        if (ValidParam.isNotEmpty(apiUserRequest.getCarLicensePlate())) {
            stringBuilder.append("车牌号：").append(apiUserRequest.getCarLicensePlate()).append(";").append("\n");
        }
        return stringBuilder.toString();
    }

    /**
     *  创建机构字段属性转化器
     *
     *  @param apiApplyParameter
     *  @return
     */
    public static ApiFieldConverter create(ApiDataExchangeParameter apiDataExchangeParameter) {
        return new ApiFieldConverter(apiDataExchangeParameter);
    }

    /**
     *  创建机构字段属性转化器
     *
     *  @param apiApplyParameter
     *  @return
     */
    public static ApiFieldConverter create(ApiApplyParameter apiApplyParameter) {
        return new ApiFieldConverter(apiApplyParameter);
    }
}
